// Decompiled by DJ v3.7.7.81 Copyright 2004 Atanas Neshkov  Date: 2010-5-24 19:45:41
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   ExpImplement.java

package nc.ui.mlr.xls;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;

import jxl.write.WritableSheet;

import org.apache.poi.hssf.usermodel.*;

public class ExpImplement {

	public ExpImplement() {
	}

	public static String buildExcel(String sheetName, String headerName[],
			String fileName, ArrayList data) {
		String errMessage;
		File file;
		FileOutputStream fOut;
		HSSFWorkbook workbook;
		errMessage = "";
		fileName = fileName.replaceAll(".xls", "");
		file = new File((new StringBuilder(String.valueOf(fileName))).append(
				".xls").toString());
		fOut = null;
		workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet(sheetName);
		sheet.setDefaultColumnWidth((short) 13);
		HSSFRow headerRow = sheet.createRow(0);
		headerRow.setHeight((short) 300);
		HSSFCellStyle cellStyle = getHeaderCellStyle(workbook);
		HSSFCell cell = null;
		for (int i = 0; i < headerName.length; i++)
			cell = getCell(headerRow, i, 1, cellStyle, headerName[i]);

		Iterator it = data.iterator();
		for (int rowNum = 1; it.hasNext(); rowNum++) {
			HSSFRow row = sheet.createRow((short) rowNum);
			row.setHeight((short) 300);
			Object cellValue[] = (Object[]) it.next();
			for (int i = 0; i < headerName.length; i++)
				cell = getCell(row, i, 1, null, cellValue[i]);
		}

		try {
			fOut = new FileOutputStream(file);
			workbook.write(fOut);
			fOut.flush();
			fOut.close();
		} catch (Exception e) {
			System.out.println((new StringBuilder("Excel\u6587\u4EF6")).append(
					file.getAbsolutePath()).append(
					"\u751F\u6210\u5931\u8D25\uFF1A").append(e).toString());
			errMessage = (new StringBuilder(String.valueOf(errMessage)))
					.append(e).toString();
			e.printStackTrace();
		}
		return errMessage;
	}

	private static HSSFCellStyle getHeaderCellStyle(HSSFWorkbook workbook) {
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setVerticalAlignment((short) 1);
		cellStyle.setWrapText(false);
		return cellStyle;
	}

	private static HSSFCell getCell(HSSFRow row, int colNum, int cellType,
			HSSFCellStyle cellStyle, Object cellValue) {
		HSSFCell cell = row.createCell((short) colNum);
		cell.setCellType(cellType);
		if (cellStyle != null)
			cell.setCellStyle(cellStyle);
		cell.setCellValue(cellValue != null ? cellValue.toString() : "");
		return cell;
	}
}